package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.service.ConcernPositionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.ConcernPosition;
import com.wanmait.wanmarecruit.mapper.ConcernPositionMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ConcernPositionServiceImpl extends ServiceImpl<ConcernPositionMapper, ConcernPosition> implements ConcernPositionService {

    @Resource
    private ConcernPositionMapper concernPositionMapper;

    @Override
    public PageInfo<ConcernPosition> selectAllCollectByUserId(int pageNum,int pageSize, Integer userId,String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<ConcernPosition> concernPositions = concernPositionMapper.selectAllCollectJobsByUserId(userId,keyword);
        return new PageInfo<>(concernPositions,5);
    }

    @Override
    public void deleteCollectJobsById(Integer concernPositionId) {
        concernPositionMapper.deleteById(concernPositionId);
    }

    @Override
    public void deleteSelectAllByCollectIds(String collectIds) {
        concernPositionMapper.deleteSelectAll(collectIds);
    }

    @Override
    public PageInfo<ConcernPosition> selectAllCollectByKeyword(int pageNum,int pageSize,Integer userId, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<ConcernPosition> concernPositions = concernPositionMapper.selectAllCollectJobsByKeyword(userId, keyword);
        return new PageInfo<>(concernPositions,5);
    }

    @Override
    public Boolean checkCollectPositionById(Integer positionId, Integer userId) {
        LambdaQueryWrapper<ConcernPosition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ConcernPosition::getUserId,userId);
        lambdaQueryWrapper.eq(ConcernPosition::getPositionId,positionId);
        return this.getOne(lambdaQueryWrapper)!=null;
    }

    @Override
    public void insertCollectPositionById(Integer positionId, Integer userId) {
        ConcernPosition concernPosition = new ConcernPosition();
        concernPosition.setPositionId(positionId);
        concernPosition.setUserId(userId);
        concernPositionMapper.insert(concernPosition);
    }

    @Override
    public void removeOneCollectByUserAndPositionId(Integer positionId, Integer userId) {
        LambdaQueryWrapper<ConcernPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConcernPosition::getPositionId,positionId);
        queryWrapper.eq(ConcernPosition::getUserId,userId);
        this.remove(queryWrapper);
    }
}
